﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SWheel : SRigidFunc<WheelJoint2D>
{ 
    JointMotor2D jointMotor;
    Rigidbody2D rigid;
    WheelJoint2D wheelJoint;
    public GameObject Connect;
    [Header("锚点")]
    public Vector3 Anchor;
    public Vector3 ConAnchor;
    public float Motor;
    public float MaxMotor;
    [Header("true为右")]
    public bool isRight;

    public override void SetRigid(Rigidbody2D r)
    {
        base.SetRigid(r);
        jointMotor = rigidComp.motor;
        jointMotor.maxMotorTorque = MaxMotor;
        rigidComp.connectedBody = Connect.transform.parent.GetComponent<Rigidbody2D>();
        rigidComp.motor = jointMotor;
        var pos = this.transform.position;
        rigidComp.connectedAnchor = rigidComp.connectedBody.transform.InverseTransformPoint(pos+Anchor);
        rigidComp.anchor = rigidComp.attachedRigidbody.transform.InverseTransformPoint(pos + Anchor);
        var suspension = rigidComp.suspension;
        suspension.dampingRatio = 1.0f;
        suspension.frequency = 100000f;
        rigidComp.suspension = suspension;


    }
    public override void LocalKeyUp()
    {
        base.LocalKeyUp();
        this.ChangeMotor(jointMotor, 0, MaxMotor,rigidComp);
    }
    public override void LocalKeyOn()
    {
        base.LocalKeyOn();
        if (isRight)
        {
            this.ChangeMotor(jointMotor, Motor, MaxMotor,rigidComp);
        }
        else
        {
            this.ChangeMotor(jointMotor, -Motor, MaxMotor,rigidComp);
        }
    }
}

